package org.maxgroundstation.world;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;

import javax.imageio.ImageIO;

/**
 * A data layer representing surface or elevation information.
 * 
 * @author Judd Reed
 * 
 * Copyright (C) 2011  Judd Reed
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class DataLayer {
    
    public static enum LAYER_TYPE { SurfaceLayerType, ElevationLayerType };
    
    public LAYER_TYPE type;
    public double lowEast, hiNorth;
    public double dEdX, dNdX, dEdY, dNdY;
    public int numRow, numCol;
    public short[] shortPixelArray;
    public int[] redGreenBlueArray;
    public double bias, gain;
    public BufferedImage orthoPhoto;
    public String imageFileName;
    public DataLayer(){}
    
    public void setTypeIsElevation(boolean isElev) {
        type = (isElev) ? LAYER_TYPE.ElevationLayerType : LAYER_TYPE.SurfaceLayerType;
    }
    
    public boolean isElevation() {
        return (type == LAYER_TYPE.ElevationLayerType);
    }
    
    public void loadSurface(String pathToPicture) {
        InputStream stream = null;
        try {
            stream = getClass().getClassLoader().getResourceAsStream(pathToPicture);
            if (stream != null) {
                
                if (pathToPicture.endsWith(".ppm")) {
                    DataInputStream dataStream = new DataInputStream(new BufferedInputStream(stream));
                    String str;
                    str = dataStream.readLine();
                    str = dataStream.readLine();
                    if(str.startsWith("#")) {
                        str = dataStream.readLine();
                    }
                    String[] wideHigh = str.split(" ");
                    numCol = Integer.parseInt(wideHigh[0]);
                    numRow = Integer.parseInt(wideHigh[1]);
                
                    str = dataStream.readLine();
             
                    redGreenBlueArray = new int[numRow*numCol];
                    orthoPhoto = null;
                    byte[] rgb = new byte[3];

                    for(int xy=0;xy<numRow*numCol;xy++) {
                        dataStream.read(rgb, 0, 3);
                        redGreenBlueArray[xy] = ((int)rgb[2]&0x0ff) 
                        + (0x0100*((int)rgb[1]&0x0ff)) + (65536*((int)rgb[0]&0x0ff));
                    }
                } else {
                    orthoPhoto = ImageIO.read(stream);
                    numRow = orthoPhoto.getHeight();
                    numCol = orthoPhoto.getWidth();
                }
            } else {
                System.err.println(pathToPicture + " could not be found.");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (stream != null) stream.close();
            }
            catch (Exception e) {
                e.printStackTrace(System.err);
            }
        }
    }
    
    public void loadElevate(String pathToPgm) {
        InputStream stream = null;
        try {
            stream = getClass().getClassLoader().getResourceAsStream(pathToPgm);
            if (stream != null) {
                DataInputStream dataStream = new DataInputStream(new BufferedInputStream(stream));
                
                String str;
                str = dataStream.readLine();
                str = dataStream.readLine();
                if(str.startsWith("#")) {
                    str = dataStream.readLine();
                }
                String[] wideHigh = str.split(" ");
                numCol = Integer.parseInt(wideHigh[0]);
                numRow = Integer.parseInt(wideHigh[1]);
                
                str = dataStream.readLine();
             
                shortPixelArray = new short[numRow*numCol];
                for(int xy=0;xy<numRow*numCol;xy++) {
                    shortPixelArray[xy] = dataStream.readShort();
                }
            } else {
                System.err.println(pathToPgm + " could not be found.");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (stream != null) stream.close();
            }
            catch (Exception e) {
                e.printStackTrace(System.err);
            }
        }
    }
}